#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<unordered_map>
using namespace std;

bool isHappy1(int n)
{
    int temp = n;
    int nnum = 0;
    int flag = 0;
    unordered_map<int, int> hash;
    while (temp)
    {
        nnum += (temp % 10) * (temp % 10);
        temp /= 10;
        if (temp == 0)
        {
            temp = nnum;
            nnum = 0;
            if (temp == 1)
            {
                flag = 1;
                break;
            }
            if (hash[temp] != 0)
            {
                break;
            }
            hash[temp]++;
        }
    }
    if (flag == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool isHappy2(int n)
{
    int slow = n - 1;
    int fast = n;
    int temp1 = n;
    int temp2 = n;
    while (slow != fast)
    {
        int count = 0;
        while (temp1)
        {
            count += (temp1 % 10) * (temp1 % 10);
            temp1 /= 10;
        }
        slow = count;
        temp1 = count;
        int num = 2;
        while (num--)
        {
            count = 0;
            while (temp2)
            {
                count += (temp2 % 10) * (temp2 % 10);
                temp2 /= 10;
            }
            fast = count;
            temp2 = count;
        }
    }
    if (slow == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}


int main()
{
    int ret = isHappy(2);
    return 0;
}